﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Architecture.Presentation.Interfaces;

namespace AddIn.EA.Architecture.Presentation.Controls
{
    public partial class BEWizard : UserControl, IAddNewBusinessEntity
    {
        private BusinessEntity _entity;

        private enum WizardStep
        { 
            Details = 0,
            Properties = 1
        }

        public BEWizard()
        {
            InitializeComponent();
            this.Load += new EventHandler(BEWizard_Load);
        }

        void BEWizard_Load(object sender, EventArgs e)
        {
            this.TabOrder = new Common.ControlTabOrder();
            this.TabOrder.OnLastTabIndex += new EventHandler<AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs>(TabOrder_OnTabIndex);
            this.TabOrder.OnFirstTabIndex += new EventHandler<AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs>(TabOrder_OnTabIndex);
            this.TabOrder.Add(btnPreview);
            this.TabOrder.Add(btnNext);
            this.TabOrder.Add(btnOK);
            this.TabOrder.MoveToFirst();

            this.ucDetails.FirstTabIndex += new Action(uc_FirstTabIndex);
            this.ucProperties.FirstTabIndex += new Action(uc_FirstTabIndex);
            this.ucDetails.LastTabIndex += new Action(uc_LastTabIndex);
            this.ucProperties.LastTabIndex += new Action(uc_LastTabIndex);

            CurrentStep = WizardStep.Details;
        }

        void uc_LastTabIndex()
        {
            this.Focus();
            this.TabOrder.MoveToFirst();
        }

        void uc_FirstTabIndex()
        {
            this.Focus();
            this.TabOrder.MoveToLast();
        }

        void TabOrder_OnTabIndex(object sender, AddIn.EA.Architecture.Presentation.Common.TabIndexEventArgs e)
        {
            if (this.CurrentStep == WizardStep.Details)
                ucDetails.Focus();
            else if (this.CurrentStep == WizardStep.Properties)
                ucProperties.Focus();
        }

        public event Action UpdateEA;

        private WizardStep CurrentStep { get; set; }

        private Common.ControlTabOrder TabOrder { get; set; }

        private IAddNewBusinessEntity BEMain
        {
            get 
            {
                try
                {
                    return (IAddNewBusinessEntity)this.Parent.Parent.Parent;
                }
                catch { return null; }
            }
        }

        #region IAddNewBusinessEntity Members

        public BusinessEntity Entity
        {
            get
            {
                if (_entity == null && BEMain != null)
                    _entity = BEMain.Entity;

                return BEMain.Entity;
            }
            set
            {
                BEMain.Entity = value;
            }
        }

        public void RefreshEntity()
        {
            if(BEMain != null)
                BEMain.RefreshEntity();
        }

        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(this.UpdateEA != null)
                this.UpdateEA();
        }

        void this_EntityValidated(bool obj)
        {        
            btnOK.Enabled = obj;

            if (btnOK.Enabled)
            {
                btnNext.Enabled = (this.CurrentStep == WizardStep.Details);
                btnPreview.Enabled = (this.CurrentStep == WizardStep.Properties);
            }
            else
            {
                btnNext.Enabled = false;
                btnPreview.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            LoadProperties();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            LoadDetails();
        }

        private void LoadDetails()
        {
            this.CurrentStep = WizardStep.Details;
            pnlMain.Controls.Clear();
            pnlMain.Controls.Add(ucDetails);
            btnPreview.Enabled = false;
            btnNext.Enabled = true;
        }

        private void LoadProperties()
        {
            this.CurrentStep = WizardStep.Properties;
            pnlMain.Controls.Clear();
            pnlMain.Controls.Add(ucProperties);
            btnPreview.Enabled = true;
            btnNext.Enabled = false;
        }
    }
}
